package jnzb.ui.event;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.table.TableModel;

import jnzb.ui.table.JTableTT;

public class EventPanel extends JPanel {

	private static final int FIXED_COL_SIZE = 60;
	private JTable eventTable;

	public EventPanel() {
		create();
	}

	private void create() {
		Border etchedBorder = BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED);
		Border titleBorder = BorderFactory.createTitledBorder(etchedBorder,
				"Events");
		this.setBorder(titleBorder);

		this.setLayout(new BorderLayout());

		// Create a new BOLD version of the current font
		Font boldFont = new Font(super.getFont().getName(), Font.BOLD, super
				.getFont().getSize());
		JLabel text = new JLabel(
				"The following events have occured during this session");
		text.setHorizontalTextPosition(JLabel.LEFT);
		text.setHorizontalAlignment(JLabel.LEFT);
		text.setFont(boldFont);
		this.add(text, BorderLayout.NORTH);

		JPanel tablePanel = createTablePanel();

		this.add(tablePanel, BorderLayout.CENTER);
	}

	private JPanel createTablePanel() {
		JPanel tablePanel = new JPanel();
		tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.X_AXIS));
		// Create empty table
		eventTable = new JTableTT();
		eventTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);		
		
		// Set preferred viewport size to current width + set height
		Dimension currentPreferred = eventTable
				.getPreferredScrollableViewportSize();

		// Show COLUMNS_HIGH colummns in the viewport
		Dimension preferredHeight = new Dimension();
		preferredHeight.setSize(currentPreferred.getWidth(), eventTable
				.getRowHeight() * 15);

		// Set preferred viewable size
		eventTable.setPreferredScrollableViewportSize(preferredHeight);

		// Add table to scroll pane
		JScrollPane scrollPane;
		scrollPane = new JScrollPane(eventTable);

		// We have a border on the panel, so remove the border on the scroll
		// panel
		scrollPane.setBorder(BorderFactory.createEmptyBorder());

		// Create height that is the same as the scroll panel + header +
		// instruction label height + padding
		Dimension panelHeight = new Dimension();
		panelHeight.setSize(preferredHeight.getWidth(), preferredHeight
				.getHeight()
				+ eventTable.getRowHeight() + 14);

		// this.setPreferredSize(panelHeight);

		tablePanel.add(scrollPane);
		
//		tablePanel.add(createControl());
		return tablePanel;
	}

	private JPanel createControl() {
		JPanel buttonsPanel = new JPanel();
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));

		JButton up = new JButton("Clear Events");
		up.setAlignmentX(0.5f);
		buttonsPanel.add(up);
		buttonsPanel.add(Box.createGlue());

		return buttonsPanel;
	}

	/**
	 * Set the table model behind the event table
	 * 
	 * @param model
	 */
	public void setModel(TableModel model) {
		this.eventTable.setModel(model);
		resizeColumns();
	}
	
	/**
	 * Resizes the columns once the table model has been set
	 */
	private void resizeColumns(){
		// TODO - might be better to be relative to preferred size
		this.eventTable.getColumnModel().getColumn(0).setMaxWidth(FIXED_COL_SIZE);
		this.eventTable.getColumnModel().getColumn(1).setMaxWidth(FIXED_COL_SIZE);
	}

}
